<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */

class WidgetTopHeader extends CWidget {
    public $showsLeft, $showRight;

    public function init() {
        $shows = MenuItem::model()->findAll(array(
            'select' => 'name,url,parent',
            'condition' => 'type = 0 and position = 1'
        ));

        $num = 0;
        $a = array();
        $n = array();

        foreach ($shows as $key => $item) {
            if ($num == 26 || $key == 0 || $key == count($shows) - 1) {
                if (count($n) > 0) {

                    if ($key == count($shows) - 1) {
                        array_push($n, array('name' => $item->name, 'url' => $item->url));
                        array_push($a, array('sub' => $n));
                    } else {
                        array_push($a, array('sub' => $n));
                    }
                    $n = array();
                }
                $num = 1;
            }
            array_push($n, array('name' => $item->name, 'url' => $item->url));
            $num++;
            $this->showsLeft = $a;
        }


        $showsRight = MenuItem::model()->findAll(array(
            'select' => 'name,url,parent',
            'condition' => 'type = 1 and position = 1'
        ));

        $this->showRight = $showsRight;
    }

    public function run() {
        return CWidget::render('widgetTopHeader', array('results' => $this->showsLeft, 'rights' => $this->showRight));
    }
}